package com.maxio.controller;

import com.maxio.domain.R;
import com.maxio.domain.Result;
import com.maxio.utils.AliOSSUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
public class UploadController {


    @Autowired
    private AliOSSUtils aliOSSUtils;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public R<?> uploadImage(MultipartFile file) throws IOException {
        if (file == null) {
            // 处理空值情况，比如返回错误信息
            log.error("上传的文件为空");
            return R.fail("上传的文件为空");
        }
        log.info("文件上传，文件名：{}", file.getOriginalFilename());   //空指针异常

        //调用阿里云oss工具类进行文件上传
        String url = aliOSSUtils.uploadImage(file);
        log.info("文件上传完成，文件访问的url:{}", url);

        return R.ok(url, "上传成功");
    }
}
